//动态创建搜索下拉dom
	var $searchInput = $('.searchInput')
	$searchInput.attr('autocomplete', 'off');
	// 创建suggest列表
	var $autocomplete = $('<div class="autocomplete"></div>').insertAfter('.searchInput').hide();
	// 清空列表
	var clear = function() {
		$autocomplete.empty().hide();
	};
	// 输入框失去焦点 ，清空
	$searchInput.blur(function() {
		setTimeout(clear, 500);
		// clear()
	});
	var selectedItem = null;
	var timeoutid = null;
	var setSelectedItem = function(item) {
		//更新索引变量 
		selectedItem = item;
		//按上下键是循环显示的，小于0就置成最大的值，大于最大值就置成0 
		if (selectedItem < 0) {
			selectedItem = $autocomplete.find('li').length - 1;
		} else if (selectedItem > $autocomplete.find('li').length - 1) {
			selectedItem = 0;
		}
		//首先移除其他列表项的高亮背景，然后再高亮当前索引的背景 
		$autocomplete.find('li').removeClass('highlight').eq(selectedItem).addClass('highlight');
	};


// 下拉显示搜索历史记录
	var search_history = function() {
		var historydata = JSON.parse(getCookie('history'))
		if (historydata) {
			for (var i = 0; i < historydata.length; i++) {
				$('<li></li>').addClass('clickable').text(historydata[i]).appendTo($('.Lists'))
			}
		}
		$autocomplete.find('li')
			.hover(function() {
				//下拉列表每一项的事件，鼠标移进去的操作 
				$(this).siblings().removeClass('highlight');
				$(this).addClass('highlight');
			}, function() {
				//下拉列表每一项的事件，鼠标离开的操作 
				$(this).removeClass('highlight');
				//当鼠标离开时索引置-1，当作标记 
				selectedItem = -1;
			})
			.click(function() {
				$searchInput.val($(this).html())
			});
		setSelectedItem(0); //默认
		$autocomplete.show()
	}


// 请求下拉数据
	function ajax_request() {
		$('<p></p>').appendTo($autocomplete)
		$('<div class="Lists"></div>').appendTo($autocomplete)
		search_history()
		var p_id = $('.Countryselect_box').attr('data-id')
		var c_id = $('.Cityselect_box').attr('data-id')
		var key_val = $searchInput.val()
		var path = get_path_params(key_val, p_id, c_id);
		$.ajax({
			url: URL + 'home/ajax_poi_autocomplete' + path,
			dataType: 'json',
			type: 'Get',
			success: function(data) {
				var data = data.content.data;
				if (data.length) {
					//遍历data，添加到自动完成区 
					$.each(data, function(index, term) {
						//创建li标签,添加到下拉列表中 
						// var ahref = $('<a></a>')
						// $('<li></li>').text(term.name).appendTo($autocomplete).addClass('clickable')
						var lihref = $('<li></li>')
						var ahref = $('<a></a>')
						var shangjia = $('<div class="search_shangjia"></div>')
						console.log(URL + getCookie('language') + '/merchant/detail/' + term.id)

						if (data[index].type == 'merchant') {
							if (getCookie('language')) {
								ahref.attr('href', URL + getCookie('language') + '/merchant/detail/' + term.id)
							} else {
								ahref.attr('href', URL + 'merchant/detail/' + term.id)
							}
						} else if (data[index].type == 'touroute') {
							if (getCookie('language')) {
								ahref.attr('href', URL + getCookie('language') + '/touroute/detail/' + term.id)
							} else {
								ahref.attr('href', URL + 'touroute/detail/' + term.id)
							}
						}
						lihref.append(ahref)
						ahref.text(term.name)
						
						if (data[index].type == 'merchant') {
							ahref.append(shangjia)
						}


						lihref.appendTo($('.Lists')).addClass('clickable')
							// 
							.hover(function() {
								//下拉列表每一项的事件，鼠标移进去的操作 
								$(this).siblings().removeClass('highlight');
								$(this).addClass('highlight');
								selectedItem = index;
							}, function() {
								//下拉列表每一项的事件，鼠标离开的操作 
								$(this).removeClass('highlight');
								//当鼠标离开时索引置-1，当作标记 
								selectedItem = -1;
							})
							.click(function() {
								//鼠标单击下拉列表的这一项的话，就将这一项的值添加到输入框中 
								$searchInput.val(term.name);
								//清空并隐藏下拉列表 
								$autocomplete.empty().hide();
							});
					});
					setSelectedItem(0); //默认
					//显示下拉列表 
					$autocomplete.show();
				}
			}
		});
	};

// 搜索输入键盘事件，操作
	$searchInput.keyup(function(event) {
			// if ($searchInput.val().length == 0) {
			// 	clear()
			// 	event.preventDefault();
			// }
			//字母数字，退格，空格 
			if (event.keyCode > 40 || event.keyCode == 8 || event.keyCode == 32) {
				//首先删除下拉列表中的信息 
				$autocomplete.empty().hide();
				clearTimeout(timeoutid);
				if ($searchInput.val().length != 0) {
					timeoutid = setTimeout(ajax_request, 0);
				}
			} else if (event.keyCode == 38) {
				console.log('上')
				//上 
				//selectedItem = -1 代表鼠标离开 
				if (selectedItem == -1) {
					setSelectedItem($autocomplete.find('li').length - 1);
				} else {
					//索引减1 
					setSelectedItem(selectedItem - 1);
				}
				event.preventDefault();
			} else if (event.keyCode == 40) {
				//下 
				//selectedItem = -1 代表鼠标离开 
				if (selectedItem == -1) {
					setSelectedItem(0);
				} else {
					//索引加1 
					setSelectedItem(selectedItem + 1);
				}
				event.preventDefault();
			}
		})
		.keypress(function(event) {
			//enter键 
			if (event.keyCode == 13) {
				//列表为空或者鼠标离开导致当前没有索引值 
				if ($autocomplete.find('li').length == 0 || selectedItem == -1) {
					return;
				}
				$searchInput.val($autocomplete.find('li').eq(selectedItem).text());
				$autocomplete.empty().hide();
				event.preventDefault();
			}
		})
		.keydown(function(event) {
			//esc键
			if (event.keyCode == 27) {
				$autocomplete.empty().hide();
				event.preventDefault();
			}
		});



// 默认显示国家列表
	var defaultid = ''
	$.ajax({
		url: URL + 'area/ajax_list_children',
		type: 'GET',
		dataType: 'json',
		data: {
			parent_id: 0
		},
		success: function(data) {
			var data = data.content;
			$('.Countrylist ul').append('<li data-id="0" li-value="0">' + all + '</li>')
			for (var i = 0; i < data.data.length; i++) {
				$('.Countrylist ul').append('<li data-id="' + data.data[i].id + '" li-value="' + data.data[i].short_name + '">' + data.data[i].short_name + '</li>')
			}
			$('.Countrylist ul').find('li').eq(0).addClass('selected')
			$('.Countryselect_box').html($('.Countrylist ul').find('li').eq(0).html());
			$('.Countryselect_box').attr('data-id', $('.Countrylist ul').find('li').eq(0).attr('data-id'))
			// var Countryselect = $('.Countryselect'); //国家
			// createSelect(Countryselect);
			defaultpid = -1;
			// defaultpid = $('.Countrylist li').eq(0).attr('data-id');
			citys(defaultpid);
		}
	})


// 国家城市dom操作
	$('.Country').mousemove(function(event) {
		$('.Countrylist').show()
	});
	$('.Country').mouseout(function(event) {
		$('.Countrylist').hide()
	});
	$('.City').mousemove(function(event) {
		$('.Citylist').show()
	});
	$('.City').mouseout(function(event) {
		$('.Citylist').hide()
	});


// 切换国家地图
	$(document).on('click', '.Countrylist li', function(event) {
		var defaultid = $(this).attr('data-id')
		if (defaultid != 0) {
			citys(defaultid)
		} else {
			$('.Cityselect_box').html(all)
			$('.Citylist ul').empty()
			$('.Cityselect_box').attr('data-id', '0')
		}
		$(this).addClass('selected').siblings().removeClass('selected');
		$('.Countryselect_box').html($(this).html())
		$('.Countryselect_box').attr('data-id', defaultid)
			// var country = $(this).attr('data-id')
			// var city = $('.Citylist li').eq(0).attr('data-id')
		// 点击国家切换地图
		var country = $(this).attr('data-id')
		var city = 0;
		var cityname = all;
		// alert(city)
		// alert(cityname)
		// show_map(country, city, cityname)
	});







// 搜索历史记录
if (!getCookie('history')) {
	var arr = []
	SetCookie('history', JSON.stringify(arr))
}else{

}


// 点击搜索按钮操作
$('.searchBtn').click(function(event) {
	var h_val = JSON.parse(getCookie('history'))
	h_val.unshift($('.searchInput').val())
	if (h_val.length > 3) {
		h_val.length = 3
	}else if(h_val.length > 3){
		h_val.length = h_val.length
	}
	var history = JSON.stringify(h_val);
	SetCookie('history', history)
	var keyword = $('.searchInput').val()
	var country = $('.Countryselect_box').attr('data-id')
	var city = $('.Cityselect_box').attr('data-id')
	var URLz = window.location.host
		// console.log(URLz)
	if($('.search_sj').hasClass('tab_search_cur')){
		// 跳转到商家
		window.location.href = 'http://' + URLz + "/merchant/search/?keyword=" + keyword + '&country=' + country + '&city=' + city
	}else{
		// 跳转到路线
		window.location.href = 'http://' + URLz + "/touroute/search/?keyword=" + keyword + '&country=' + country + '&city=' + city
	}
	
});


 //搜索页面处理null 
   var keyword_v = decodeURIComponent((new RegExp('[?|&]keyword=' + '([^&;]+?)(&|#|;|$)', "ig").exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null;
   var country_v = decodeURIComponent((new RegExp('[?|&]country=' + '([^&;]+?)(&|#|;|$)', "ig").exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null;
   var city_v = decodeURIComponent((new RegExp('[?|&]city=' + '([^&;]+?)(&|#|;|$)', "ig").exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null;
   if (keyword_v == null) {
   	$('.searchInput').val('')
   } else {
   	$('.searchInput').val(keyword_v)
   }
   $('.Countryselect_box').html($(".Countrylist").find("li[data-id=" + country_v + "]").html())
// console.log($(".Countrylist").find("li[data-id=" + country_v +"]").html())